import json
from typing import Dict
import pickle
def save_var_cache(var, file):
    """
    读取本地缓存
    :param var: 
    :param file: 
    :return: 
    """
    f = open(file, 'wb')
    pickle.dump(var, f)
    f.close()

def read_cache(file):
    """
    读取本地
    :param file: 
    :return: 
    """
    f = open(file, 'rb')
    d = pickle.load(f)
    f.close()
    return d

def read_json(file_path:str, encoding='utf-8'):
    """
    读取json 
    :param file_path: 
    :param encoding: 
    :return: 
    """
    with open(file_path, 'r', encoding=encoding) as file:
        data = json.load(file)
    return data

def write_json(dict_ob: Dict, file_path: str):
    """
    将一个Python字典对象写入json文件中
    Args:
        dict_ob: dict，待写入数据的Python字典对象
        file_path: str，待写入数据的json文件的路径

    Raises:
        IOError，无法打开或写入json文件
        TypeError，无法序列化Python字典为合法的json
    """
    with open(file_path, "w", encoding='utf-8') as f:
        json.dump(dict_ob, f, ensure_ascii=False, indent=1)
